home *** CD-ROM | disk | FTP | other *** search
- Path: abacus.abasoft.co.uk!not-for-mail
- From: dmb@abacus.abasoft.co.uk (David Byrne)
- Newsgroups: comp.lang.c++
- Subject: Re: Using cin like sscanf()?
- Date: 4 Mar 1996 17:20:12 -0000
- Organization: Abacus Software Ltd.
- Message-ID: <4hf8oc$47t@abacus.abasoft.co.uk>
- References: <4gnudd$dd4@daily-planet.nodak.edu>
- NNTP-Posting-Host: abacus.abasoft.co.uk
- X-NNTP-Posting-Host: abacus.demon.co.uk
-
- In article <4gnudd$dd4@daily-planet.nodak.edu>,
- Jeffro <ishaq@plains.nodak.edu> wrote:
- >greetings! I've always wondered if this is possible. Well, I'm sure
- >it's possible, but how is it done? Say I input a string into string:
- >
- >char string[] = "This is a character string.";
- >char buffer[24];
- >
- >How can I tell cin to read from string instead of stdin so I can do
- >soemthing like this:
- >
- >//cin.read_from(string)
- >while (cin >> buffer) cout << buffer; //Outputs string
- >//cin.read_from(stdin)
-
- Some day (maybe soon), I'm gonna be embarrassed by this attempt !!
- Anyway here goes...
-
- #include <iostream.h>
- #include <strstream.h>
-
- int main(void)
- {
- char a[50];
- char string[] = "This is a character string.";
- istrstream i(string);
- char c;
-
- do {
- i >> a;
- cout << a;
- } while (!i.rdstate());
- return 0;
- }
- --
- David Byrne, Abacus Software, London, UK Tel: +44 (0)171 603 9877
- Email: dmb@abacus.demon.co.uk Fax: +44 (0)171 603 6844
- Here's a koan: If you have ice-cream I will give you some. If you have none,
- I will take it away from you. (it's an ice-cream koan).
-